package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.base.response.JsonResult;
import cn.tedu.egweibo1.base.response.StatusCode;
import cn.tedu.egweibo1.mapper.UserMapper;
import cn.tedu.egweibo1.pojo.dto.UserAddParam;
import cn.tedu.egweibo1.pojo.entity.User;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
@Tag(name= "01-用户管理模块")
@Slf4j
@RestController
@RequestMapping("v1/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.添加用户功能
     */
    @PostMapping("/add")
    @Parameters(value = {
            @Parameter(name = "username",description = "用户名",required = true),
            @Parameter(name = "password",description = "密码",required = true),
            @Parameter(name = "nickname",description = "昵称",required = true),
            @Parameter(name = "userAddParam",hidden = true)
    })
    public JsonResult add(@Validated @RequestBody UserAddParam userAddParam){
        log.debug("userAddParam="+userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam, user);
        user.setCreateTime(new Date());
        log.debug("user"+ user);
        int num = userMapper.insertUser(user);
        if(num > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
}
